package com.oto.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.stereotype.Service;

import com.oto.model.MessageDO;

/**
 * @author xueqiang.mi
 * @since 2010-12-19
 */
@Service
public class MessageService extends BaseService {

	public void store(String content) {
		MessageDO obj = new MessageDO();
		obj.setContent(content);
		obj.setCrtDate(new Date());

		save(obj);
	}

	public MessageDO get(Long id) {
		return get(MessageDO.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<MessageDO> lattest() {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(MessageDO.class);
			query.setOrdering("crtDate desc");
			query.setRange(0, 30);
			Collection<MessageDO> c = pm.detachCopyAll((Collection<MessageDO>) query.execute());
			return new ArrayList<MessageDO>(c);
		} finally {
			pm.close();
		}
	}
}
